var socket;
window.onload = function () {
    if(typeof(WebSocket) == "undefined") {
        setTimeout(function () {
            layui.layer.msg("您的浏览器不支持 WebSocket 将无法接收到消息！");
        },2000);
        return;
    }
    socket = new WebSocket("ws://"+layui.cache.host+"websocket/"+systemUser.oid);
    //打开事件
    socket.onopen = function() {
        console.log("WebSocket 链接成功");
        //socket.send("这是来自客户端的消息" + location.href + new Date());
    };
    //获得消息事件
    socket.onmessage = function(msg) {
        var data = JSON.parse(msg.data);
        var json = {
            img:data.img,
            imageWidth:data.imageWidth,
            message:data.message,
            icon:data.icon,
            close:data.close,
            title:data.title
        };
        if(data.type == 1){
            top.layui.notice.msg(json);
        }else if(data.type == 2){
            top.layui.notice.success(json);
        }else if(data.type == 3){
            top.layui.notice.error(json);
        }else if(data.type == 4){
            top.layui.notice.warning(json);
        }else if(data.type == 5){
            top.layui.notice.info(json);
        }else if(data.type == 5){
            top.layui.notice.show(json);
        }

        //发现消息进入    调后台获取
    };
    //关闭事件
    socket.onclose = function() {
        console.log("WebSocket  已关闭");
    };
    //发生了错误事件
    socket.onerror = function() {
        setTimeout(function () {
            layui.layer.msg("WebSocket 发生了错误 将无法接收到消息！");
        },2000);
    };

};
